﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace CoiffeurProject.UserControls
{
    internal class CPScrollViewer
    {
        ScrollViewer scrollViewer;
        Grid content;

        internal CPScrollViewer(Thickness margin, int width, int height, int column, int row)
        {
            scrollViewer = new ScrollViewer()
            {
                Height = height,
                Width = width,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = margin
            };

            content = new Grid()
            {
                Width = width - 30,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            scrollViewer.Content = content;

            Grid.SetRow(scrollViewer, row);
            Grid.SetColumn(scrollViewer, column);
        }

        internal CPScrollViewer(Thickness margin, int width, int height, int column, int row, int columnspan, int rowspan)
            : this(margin, width, height, column, row)
        {
            if (columnspan > 0) Grid.SetColumnSpan(scrollViewer, columnspan);
            if (rowspan > 0) Grid.SetRowSpan(scrollViewer, rowspan);
        }

        internal void MoveToYCoord(int y_coordinate)
        {
            scrollViewer.ScrollToVerticalOffset(y_coordinate);
        }

        internal void UpdateScrollViewer(List<ScrollViewerEntry> entries)
        {
            ResetEntries();

            for (int i = 0; i < entries.Count; i++)
                AddEntry(entries[i]);
        }

        internal void AddEntry(ScrollViewerEntry entry)
        {
            content.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(entry.MainContainer.Height) });
            int rows_num = content.RowDefinitions.Count;

            Grid.SetRow(entry.MainContainer, rows_num - 1);
            content.Children.Add(entry.MainContainer);
        }

        internal void AddEntry(ScrollViewerNotificationEntry entry)
        {
            content.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(entry.MainContainer.Height) });
            int rows_num = content.RowDefinitions.Count;

            Grid.SetRow(entry.MainContainer, rows_num - 1);
            content.Children.Add(entry.MainContainer);
        }

        internal void ResetEntries()
        {
            content.Children.Clear();
            content.RowDefinitions.Clear();
        }

        internal ScrollViewer ScrollViewer
        {
            get { return scrollViewer; }
        }
    }
}
